import {Component} from '@angular/core';
import {AlertController, IonicPage, ModalController, NavController, NavParams, ViewController} from 'ionic-angular';
import {ApplySucceedConfirmPage} from "../apply-succeed-confirm/apply-succeed-confirm";
import {APARTMENTAPISERVICE, BuildingModel} from "../../api/apartment-api.service";
import {LocalStorage} from "../../utils/localstorage.utils";
import {USER} from "../../service/user.service";
import {GoodModel} from "../../model/good.model";
import {GOODS} from "../../api/goods-api";

/**
 * Generated class for the ExchangeRoomConfirmPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-gift-confirm',
  templateUrl: 'gift-confirm.html',
})
export class GiftConfirmPage {
  data={
    layoutId:undefined
  };
  layoutList=[];
  good:GoodModel;


  constructor(public navCtrl:NavController,
              public navParams:NavParams,
              private viewCtrl:ViewController,
              private modalCtrl:ModalController,
              protected GOODS:GOODS,
              private USER:USER,
              private ls:LocalStorage,
              private apartmentApiService:APARTMENTAPISERVICE) {
    this.good = this.ls.getObject('good');
  }

  ionViewDidLoad() {
    this.loadMyHouse();
  }

  dismiss() {
    console.log("dismiss");
    this.viewCtrl.dismiss();
  }
  goExchange() {
    let reqData = {
      totalMoney:0,
      houseId: this.data.layoutId,
      goodsId: this.good.id,
      source: 1,
      score:this.good.score
    }
    this.GOODS.purchaseScore(reqData,v=>{
      this.USER.toast("兑换成功，请去兑换记录中查看")
      this.dismiss()
    })
    // this.ROUTER.go(ROUTER.CHECK);
  }

  prevent(e) {
    e.stopPropagation();
  }

  loadMyHouse() {
    this.apartmentApiService.getMyRentingHouse({}, (res:any)=> {
      this.layoutList = res;
      if(this.layoutList.length > 0){
        this.data.layoutId = this.layoutList[0].id
      }
    })
  }

  doCheckout() {
    this.dismiss();
    let profileModal = this.modalCtrl.create(ApplySucceedConfirmPage, this.data, {showBackdrop: true});
    profileModal.present();
  }
}
